import { createRouter, createWebHistory } from 'vue-router';

import Login from '@/views/login/loginView.vue';
import Layout from '@/views/layout/layOut.vue';
import Home from '@/views/home/homeView.vue';


const templateRoutes = [
  {
    path: '/',
    name: 'layout',
    redirect: '/home',
    meta: {
      title: '首页'
    },
    component: Layout,
    children: [
      {
        path: '/home',
        name: 'home',
        meta: {
          title: '首页',
          level: 1 // 如果是一级菜单请加上 level:1 然后面包屑会自动处理
        },
        component: Home
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录',
      level: 1
    },
    component: Login
  },
  // 配置404
  {
    path: '/:pathMatch(.*)*',
    name: 'notfound',
    component: () => import('@/views/notFound/notFound.vue'),
    meta: {
      title: '您访问的页面不存在(404)',
      level: 1
    }
  }
];

export let routes = [];


// 只有localhost才会显示模板菜单
if (location.hostname === "localhost") {
  routes = [...routes, ...templateRoutes];
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
});

router.beforeEach((to) => {
  const token = localStorage.getItem('token');
  if (!token && to.name !== 'login') {
    return { name: 'login' };
  }
});

router.afterEach((to) => {
  document.title = to.meta.title;
});

export default router;
